package com.muyu.product.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.muyu.common.core.text.Convert;
import com.muyu.product.domain.model.RuleInfoAddModel;
import com.muyu.product.domain.resp.RuleInfoResp;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.product.domain.RuleInfo;
import com.muyu.product.domain.req.RuleInfoQueryReq;
import com.muyu.product.domain.req.RuleInfoSaveReq;
import com.muyu.product.domain.req.RuleInfoEditReq;
import com.muyu.product.service.RuleInfoService;
import com.muyu.common.core.web.page.TableDataInfo;

/**
 * 商品规格Controller
 *
 * @author DongZeLiang
 * @date 2024-02-27
 */
@Api(tags = "商品规格")
@RestController
@RequestMapping("/rule")
public class RuleInfoController extends BaseController {
    @Autowired
    private RuleInfoService ruleInfoService;

    /**
     * 查询商品规格列表
     */
    @ApiOperation("获取商品规格列表")
    @RequiresPermissions("product:rule:list")
    @GetMapping("/list")
    public Result list(RuleInfoQueryReq ruleInfoQueryReq) {
        boolean isPage = ruleInfoQueryReq.getParams().get("isPage") == null || Convert.toBool(ruleInfoQueryReq.getParams().get("isPage"), true);
        if (isPage){
            startPage();
        }
        TableDataInfo<RuleInfoResp> tableDataInfo = ruleInfoService.queryList(ruleInfoQueryReq);
        return isPage ? Result.success(tableDataInfo) : Result.success(tableDataInfo.getRows());
    }

    /**
     * 导出商品规格列表
     */
    @ApiOperation("导出商品规格列表")
    @RequiresPermissions("product:rule:export")
    @Log(title = "商品规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RuleInfo ruleInfo) {
        List<RuleInfo> list = ruleInfoService.list(ruleInfo);
        ExcelUtil<RuleInfo> util = new ExcelUtil<RuleInfo>(RuleInfo.class);
        util.exportExcel(response, list, "商品规格数据");
    }

    /**
     * 获取商品规格详细信息
     */
    @ApiOperation("获取商品规格详细信息")
    @RequiresPermissions("product:rule:query")
    @GetMapping(value = "/{id}")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    public Result<RuleInfo> getInfo(@PathVariable("id") Long id) {
        return Result.success(ruleInfoService.getById(id));
    }

    /**
     * 新增商品规格
     */
    @RequiresPermissions("product:rule:add")
    @Log(title = "商品规格", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增商品规格")
    public Result<String> add(@RequestBody RuleInfoSaveReq ruleInfoSaveReq) {
        return toAjax(ruleInfoService.save(RuleInfoAddModel.saveReqBuild(ruleInfoSaveReq)));
    }

    /**
     * 修改商品规格
     */
    @RequiresPermissions("product:rule:edit")
    @Log(title = "商品规格", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    @ApiOperation("修改商品规格")
    public Result<String> edit(@PathVariable Long id, @RequestBody RuleInfoEditReq ruleInfoEditReq) {
        return toAjax(ruleInfoService.updateById(RuleInfo.editBuild(id,ruleInfoEditReq)));
    }

    /**
     * 删除商品规格
     */
    @RequiresPermissions("product:rule:remove")
    @Log(title = "商品规格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除商品规格")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = String.class, example = "1,2,3,4")
    public Result<String> remove(@PathVariable List<Long> ids) {
        return toAjax(ruleInfoService.removeBatchByIds(ids));
    }
}
